package com.dreamwhale.machinery.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName : UploadFile
 * @Description : 上传本地的接口
 * @Author : DragonUncle
 * @Date :  11:29 2020/3/14
 */
@Component
public class UploadFile {
    @Value("${static.file.path}")
    private String ImgPath;

    /**
     * @param : null
     * @return :
     * @MethodName : writePicture
     * @Description : 上传多张图片
     * @Author : DragonUncle
     * @Data : 14:30 2020/3/14
     */
    public List<String> writePicture(MultipartFile[] files, String packetName) {
        List<String> fileNames = new ArrayList<>();
        for (MultipartFile file : files) {
            String fileName = writePicture(file, packetName);
            if (StringUtils.isNotBlank(fileName)) fileNames.add(fileName);
        }
        return fileNames;
    }

    /**
     * @param : fileByte 文件的字节码
     * @param : fileName 文件的名字
     * @param : filePath 文件存放的路径
     * @return : 写出是否成功
     * @MethodName : writeFile
     * @Description : 通用写出文件
     * @Author : DragonUncle
     * @Data : 11:36 2020/3/14
     */
    private Boolean writeFile(byte[] fileByte, String fileName, String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.isDirectory()) if (!file.mkdirs()) return false;
        File newFile = new File(filePath + fileName);
        FileCopyUtils.copy(fileByte, newFile);
        return true;
    }

    /**
     * @param : fileByte    文件字节
     * @param : fileName    文件名字
     * @param : packetName  文件的报名
     * @MethodName :writePicture
     * @Description : 写图片到本地
     * @Author : DragonUncle
     * @Data : 11:48 2020/3/14
     */
    public String writePicture(MultipartFile file, String fileName) {
        String postfix;
        if (file != null) {
            String contentType = file.getContentType();
            if (StringUtils.isBlank(contentType)) {
                String originalFilename = file.getOriginalFilename();
                if (StringUtils.isBlank(originalFilename)) originalFilename = "xxx.find";
                postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
            } else {
                String[] split = contentType.split("/");
                postfix = split[1];
            }
            if (StringUtils.isBlank(fileName))
                fileName = UUID.randomUUID().toString() + "." + postfix;
            try {
                if (writeFile(file.getBytes(), fileName, ImgPath + fileName))
                    return "http://47.103.22.134:8080/img/" + fileName;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
